package com.mrx.agent.advice;

import net.bytebuddy.asm.Advice;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Mr.X
 * @since 2024-11-05 11:16
 **/
public class ProcessBuilderAdvice {

    public static final Set<String> args = new HashSet<>();

    @Advice.OnMethodEnter
    public static void onEnter(@Advice.Argument(value = 0, readOnly = false) List<String> commands) {
        commands = new ArrayList<>(commands);
        log("Original command list: " + commands);
        for (String arg : args) {
            commands.add(1, arg);
        }
        log("args: " + args);
        log("Modified command list: " + commands);
    }

    public static void log(String msg) {
        System.out.println(ProcessBuilderAdvice.class.getSimpleName() + ": " + msg);
    }

    public static void addArgs(Set<String> args) {
        ProcessBuilderAdvice.args.addAll(args);
    }

}

